package com.jiangnan.ui.activity

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.chad.library.adapter4.BaseQuickAdapter
import com.jiangnan.R
import com.jiangnan.databinding.ActivityCommentsBinding
import com.jiangnan.ui.adapter.BorrowOrReturnBookQuickAdapter
import com.jiangnan.ui.adapter.CommentsAdapter
import com.jiangnan.util.GlideUtil
import com.jiangnan.util.SharedPrefUtil
import com.jiangnan.viewmodel.CommentViewModel
import com.jiangnan.viewmodel.UserViewModel
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout


class CommentsActivity : BaseActivity() {
    private lateinit var binding: ActivityCommentsBinding
    private lateinit var vm: CommentViewModel
    private lateinit var userViewModel: UserViewModel
    private lateinit var adapter: CommentsAdapter

    // TODO：从你的登录模块获取当前用户 ID
    private var currentUserId : Long? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityCommentsBinding.inflate(layoutInflater)
        setContentView(binding.root)

        currentUserId = SharedPrefUtil.getLoginInfo(this)!!.id.toLong()

        vm = ViewModelProvider(this)[CommentViewModel::class.java]
        userViewModel = ViewModelProvider(this)[UserViewModel::class.java]

        userViewModel.loadUser(currentUserId!!)

        adapter = CommentsAdapter(
            currentUserId!!,
            onReply = { parentId, toUserId, toUserName ->
                vm.currentParentId = parentId
                vm.currentToUserId = toUserId
                binding.etContent.hint = "回复 @$toUserName"
            },
            onDelete = { id ->
                vm.deleteComment(id)
            }
        )

        val refreshLayout = findViewById<View>(com.jiangnan.R.id.refreshLayout) as RefreshLayout
        refreshLayout.setRefreshHeader(ClassicsHeader(this))
//        refreshLayout.setRefreshFooter(ClassicsFooter(this))
        refreshLayout.setOnRefreshListener { refreshlayout ->
            vm.loadComments()
            refreshlayout.finishRefresh(true) //传入false表示刷新失败
        }
//        refreshLayout.setOnLoadMoreListener { refreshlayout ->
//            refreshlayout.finishLoadMore(2000 /*,false*/) //传入false表示加载失败
//        }

        var recyclerView = binding.recyclerView
        recyclerView.layoutManager = StaggeredGridLayoutManager(
            1, StaggeredGridLayoutManager.VERTICAL
        )
        recyclerView.adapter = adapter

        vm.comments.observe(this) { adapter.submit(it) }
        vm.error.observe(this) {
            Toast.makeText(this, it, Toast.LENGTH_SHORT).show()
        }
        userViewModel.user.observe(this) {
            GlideUtil.loadCircularImage(
                this,
                it!!.avatar!!,
                R.drawable.img_default,
                R.drawable.img_error,
                binding.ivAvatar
            )
        }

        vm.loadComments()

        binding.btnSend.setOnClickListener {
            val txt = binding.etContent.text.toString().trim()
            if (txt.isEmpty()) {
                Toast.makeText(this, "请输入评论", Toast.LENGTH_SHORT).show()
            } else {
                vm.postComment(currentUserId!!, txt)
                binding.etContent.text?.clear()
                binding.etContent.hint = "写下你的评论..."
            }
        }
    }
}
